package org.nvm.core;

public class Lexeme implements Comparable<Lexeme> {

	//lexemeType常量
	//未知
	public static final int TYPE_UNKNOWN = 0;
	//英文
	public static final int TYPE_ENGLISH = 1;
	//数字
	public static final int TYPE_ARABIC = 2;
	//英文数字混合
	public static final int TYPE_LETTER = 3;
	//中文词元
	public static final int TYPE_CNWORD = 4;
	//中文单字
	public static final int TYPE_CNCHAR = 64;
	//日韩文字
	public static final int TYPE_OTHER_CJK = 8;
	//中文数词
	public static final int TYPE_CNUM = 16;
	//中文量词
	public static final int TYPE_COUNT = 32;
	//中文数量词
	public static final int TYPE_CQUAN = 48;
	
	private int offset;
	private int begin;
	private int length;
	private String lexemeText;
	private int type;

	public Lexeme(int offset, int begin, int length, int type) {
		super();
		if(length < 0) {
			throw new IllegalArgumentException("length < 0");
		}
		this.offset = offset;
		this.begin = begin;
		this.length = length;
		this.type = type;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + begin;
		result = prime * result + length;
		result = prime * result + offset;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if(this == obj) {
			return true;
		}
		if(obj instanceof Lexeme) {
			Lexeme o = (Lexeme) obj;
			return this.offset == o.offset
					&& this.begin == o.begin
					&& this.length == o.length;
		}
		return false;
	}



	public int getOffset() {
		return offset;
	}
	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getBegin() {
		return begin;
	}
	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		if(length < 0) {
			throw new IllegalArgumentException("length < 0");
		}
		this.length = length;
	}

	public String getLexemeText() {
		if(lexemeText == null) {
			return "";
		}
		return lexemeText;
	}
	public void setLexemeText(String lexemeText) {
		this.lexemeText = lexemeText;
	}

	public int getBeginPosition() {
		return this.offset + this.begin;
	}
	public int getEndPosition() {
		return this.offset + this.begin + this.length;
	}

	public int getType() {
		return type;
	}
	public String getLexemeTypeString(){
		switch(type) {
		case TYPE_ENGLISH :
			return "ENGLISH";
			
		case TYPE_ARABIC :
			return "ARABIC";
			
		case TYPE_LETTER :
			return "LETTER";
			
		case TYPE_CNWORD : 
			return "CN_WORD";
			
		case TYPE_CNCHAR : 
			return "CN_CHAR";
			
		case TYPE_OTHER_CJK :
			return "OTHER_CJK";
			
		case TYPE_COUNT :
			return "COUNT";
			
		case TYPE_CNUM :
			return "TYPE_CNUM";
			
		case TYPE_CQUAN:	
			return "TYPE_CQUAN";
			
		default :
			return "UNKONW";
		}
	}

	public void setType(int type) {
		this.type = type;
	}

	public boolean append(Lexeme l, int type) {
		if(l != null && this.getEndPosition() == l.getBeginPosition()) {
			this.length += l.length;
			this.type = l.type;
			return true;
		}
		return false;
	}

	@Override
	public int compareTo(Lexeme o) {
		if(this.begin < o.begin) {
			return -1;
		} else if(this.begin == o.begin) {
			if(this.length > o.length) {
				return -1;
			} else if(this.length == o.length) {
				return 0;
			} else {
				return 1;
			}
		}
		return 1;
	}
}
